package com.ztesoft.web.common.db.dao;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.ztesoft.core.common.Page;
import com.ztesoft.framework.exception.SysRuntimeException;
import com.ztesoft.framework.util.StringUtils;
import com.ztesoft.web.common.db.arg.DmArg;
import com.ztesoft.web.common.db.arg.DmArg.DmCriteria;
import com.ztesoft.web.common.db.dao.mapper.IDmMapper;
import com.ztesoft.web.common.db.po.DmPO;

@Repository
public class DmDao extends SqlSessionDaoSupport {

    @Resource(name = "majorSqlSessionTemplate")
    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        super.setSqlSessionTemplate(sqlSessionTemplate);
    }

    public List<Integer> countByArg(DmArg arg) {
        return getMapper().countByArg(arg);
    }

    public int deleteByArg(String subTableName,DmArg arg) {
        return getMapper().deleteByArg(subTableName,arg);
    }
    
    public int updateBySctArg(String subTableName, DmPO record, DmArg arg) {
    	return getMapper().updateBySctArg(subTableName, record, arg);
    }

    public List<DmPO> selectByArg(DmArg arg) {
        return getMapper().selectByArg(arg);
    }
    
  	public List<DmPO> selectByArgAndSize(String subTableName,DmArg arg,int limits){
  		return getMapper().selectByArgAndSize(subTableName,arg,limits);
  	}
   
    public int updateByArgSelective(DmPO record, DmArg arg) {
        return getMapper().updateByArgSelective(record, arg);
    }

    public int updateByArg(DmPO record, DmArg arg) {
        return getMapper().updateByArg(record, arg);
    }

    public Page<DmPO> selectByArgAndPage(String subTablename,DmArg arg,
            Page<DmPO> resultPage) {
        List<DmPO> resultList = getMapper().selectByArgAndPage(subTablename,arg,
                resultPage);
        resultPage.setResultList(resultList);
        return resultPage;
    }

    public int insert(DmPO record) {
        return getMapper().insert(record);
    }

    public int insertSelective(DmPO record) {
        return getMapper().insertSelective(record);
    }

    public int insertBatch(String taleName,List<DmPO> records) {
        return getMapper().insertBatch(taleName, records);
    }

    public int deleteByPrimaryKey(Long key) {
        return getMapper().deleteByPrimaryKey(key);
    }

    public DmPO selectByPrimaryKey(Long key) {
        return getMapper().selectByPrimaryKey(key);
    }

    public int updateByPrimaryKeySelective(DmPO record) {
        return getMapper().updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(String tableName, DmPO record) {
        return getMapper().updateByPrimaryKey(tableName, record);
    }

    /**
     * 根据传入的Map条件进行查询，当前仅支持所有Map中Key字段的EqualTo查询
     * @param params Map,Key=字段名，value=查询值
     * @return
     */
    public List<DmPO> selectByMap(Map<String, Object> params) {
        return (selectByArg(buildQueryObject(params)));
    }

	/**
	 * 获取需要恢复数据的目标表名
	 * @param tableName
	 * @return
	 */
	public List<DmPO> selectRDTargetTableName(String tableName,Integer id) {
		return getMapper().selectRDTargetTableName(tableName,id);
	}
	
	/**
	 * 获取需要恢复数据的数据
	 * 
	 * @param tableName
	 * @return
	 */
	public List<DmPO> selectDataForRD(String tableName, Integer id, String targetTableName, Integer limit) {
		return getMapper().selectDataForRD(tableName, id, targetTableName, limit);
	}

    private DmArg buildQueryObject(Map<String, Object> params) {

        DmArg arg = new DmArg();
        DmCriteria criteria = arg.createCriteria();

        Class criteriaClass = criteria.getClass();
        Set keys = params.keySet();

        if (keys != null) {

            Iterator iterator = keys.iterator();

            while (iterator.hasNext()) {

                Object key = iterator.next();
                Object value = params.get(key);
                for (Method method : criteriaClass.getMethods()) {
                    if (method.getName().equals(
                            "and"+ StringUtils.toUpperCaseFirstOne(key.toString()) + "EqualTo")) {
                        try {
                            method.invoke(criteria, value);
                        }
                        catch (Exception e) {
                            throw new SysRuntimeException(e);
                        }
                        break;
                    }
                }
            }
        }
        return arg;
    }
    
	public int updateByArg(DmPO record, String tableName, DmArg dmArg) {
		return getMapper().updateByArgAndSize(record, tableName,dmArg);
		
	}
	
    public IDmMapper getMapper() {
    	return getSqlSession().getMapper(IDmMapper.class);
    }
	public int deleteByDetailInfo(String tableName, int dmTaskId){
		return getMapper().deleteByDetailInfo(tableName, dmTaskId);
	}
	
    
    public List<DmPO> selectByLimit(String table, int taskId, int strategyId, String state, Integer limit) {
    	return getMapper().selectByLimit(table, taskId, strategyId, state, limit);
    }
    
    
    public List<DmPO> selectByLimit(String table, int taskId, int strategyId, String state, Integer start, Integer limit) {
    	return getMapper().selectByLimit_(table, taskId, strategyId, state, start, limit);
    }
    
    public int updateByLimit(String table, String state_, Date stateDate, int taskId, int strategyId, 
    		String state, Integer limit, String errDesc) {
    	return getMapper().updateByLimit(table.toLowerCase(), state_, stateDate, taskId, strategyId, state, limit, errDesc);
    }
    
    public int updateByLimit_(String table, String state_, Date stateDate, int taskId, int strategyId, 
    		String state, Integer limit, String errDesc) {
    	return getMapper().updateByLimit(table.toLowerCase(), state_, stateDate, taskId, strategyId, state, limit, errDesc);
    }
    public List<DmPO> qryLeftErrorInfo(String table, int dmTaskId,String state) {
    	return getMapper().qryLeftErrorInfo(table, dmTaskId, state);
    }
    
    public List<DmPO> qryRightErrorInfo(String table, int dmTaskId,String state) {
    	return getMapper().qryRightErrorInfo(table, dmTaskId, state);
    }

	/*public int updateByIds(DmPO record, String string, List<Long> keyList) {
		return getMapper().updateByIds(DmPO record, String string,  keyList);
		
	}*/
    
}
